

package com.hazelcast.config.test.builders;

import java.util.HashMap;
import java.util.Map;

public class ConfigReplacerBuilder {
    private String className;
    private Map<String, String> properties = new HashMap<>();

    private ConfigReplacerBuilder withClassName(String className) {
        this.className = className;
        return this;
    }

    public ConfigReplacerBuilder withClass(Class<?> c) {
        return withClassName(c.getName());
    }

    public ConfigReplacerBuilder addProperty(String key, boolean value) {
        return addProperty(key, String.valueOf(value));
    }

    public ConfigReplacerBuilder addProperty(String key, int value) {
        return addProperty(key, String.valueOf(value));
    }

    public ConfigReplacerBuilder addProperty(String key, String value) {
        properties.put(key, value);
        return this;
    }

    public String build() {
        if (properties.isEmpty()) {
            return "<replacer class-name='" + className + "' />\n";
        } else {
            return "<replacer class-name='" + className + "'>\n" + "<properties>" + properties() + "</properties>" + "</replacer>";
        }
    }

    private String properties() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> property : properties.entrySet()) {
            sb.append("<property name='").append(property.getKey()).append("'>").append(property.getValue()).append("</property>\n");
        }
        return sb.toString();
    }
}
